package com.gujiangbo.service.impl;

import com.github.javafaker.Faker;
import com.gujiangbo.constant.CommonsConstant;
import com.gujiangbo.constant.IDUtils;
import com.gujiangbo.dao.SchoolmateDaoService;
import com.gujiangbo.entity.Schoolmate;
import com.gujiangbo.request.BatchGenerateCondition;
import com.gujiangbo.request.SchoolmateRequest;
import com.gujiangbo.response.SchoolmateResponse;
import com.gujiangbo.service.SchoolmateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2021/12/7 20:47
 * @Version: 1.0
 * @Description:
 */
@Service
@Slf4j
public class SchoolmateServiceImpl implements SchoolmateService {

    @Resource
    private SchoolmateDaoService schoolmateDaoService;

    @Resource
    private AsyncTaskExecutor asyncThreadPoolTaskExecutor;

    @Override
    public void batchGenerateSchoolmates(BatchGenerateCondition condition) {
        log.info("开始批量生成...");
        int batchSize = condition.getBatchNo();
        Faker faker = new Faker();
        List<Schoolmate> list = new ArrayList<>();
        IntStream.range(0, batchSize).forEach(v -> {
            Schoolmate schoolmate = new Schoolmate();
            schoolmate.setId(IDUtils.getId());
            schoolmate.setName(faker.name().name());
            schoolmate.setPet(faker.animal().name());
            schoolmate.setAddress(faker.address().fullAddress());
            schoolmate.setBook(faker.book().title());
            schoolmate.setMusic(faker.music().chord());
            schoolmate.setPhone(faker.phoneNumber().phoneNumber());
            list.add(schoolmate);
        });
        //schoolmateDaoService.batchGenSchoolmates(list, batchSize);
        asyncThreadPoolTaskExecutor.submit(new Thread(() -> schoolmateDaoService.batchGenSchoolmates(list, batchSize)));
    }

    @Override
    public SchoolmateResponse getSchoolmate(SchoolmateRequest request) {
        SchoolmateResponse response = new SchoolmateResponse();
        Schoolmate schoolmate = schoolmateDaoService.getSchoolmate(request);
        if (!ObjectUtils.isEmpty(schoolmate)) response.setSchoolmate(schoolmate);
        response.setCode(CommonsConstant.SUCCESS_CODE);
        response.setMessage(CommonsConstant.SUCCESS_MSG);
        return response;
    }

    @Override
    public Schoolmate querySchoolmatesLikeByID(String id) {
        SchoolmateRequest request = new SchoolmateRequest();
        request.setId(id);
        return schoolmateDaoService.getSchoolmate(request);
    }
}
